﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Triggers.CUiCreateTriggerDlg
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Resources;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Olp;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.Triggers;

public class CUiCreateTriggerDlg : TxForm
{
  private CCoCreateTriggerCmd m_cmd;
  private TxPlacementManipulator m_placementManipulator;
  private ITxDisplayableObject m_secondLocation;
  private ITxDisplayableObject m_parentLocation;
  private ITxLocationOperation m_origSelectedLocation;
  private bool _doUpdate = true;
  internal TriggerAction m_action;
  private ITxLocatableObject m_manipulator;
  private bool m_IsTriggerNameChanged;
  private string m_origTriggerName;
  private string m_origTriggerBrushName;
  private TxTransformation m_origTriggerTransformation;
  private ITxRoboticTriggerParentLocation m_origParentLocation;
  private TxRoboticPaintTrigger m_origTrigger;
  private IContainer components;
  private Label label7;
  private ComboBox TriggerType;
  private Label TypeLable;
  private Button OK;
  private Button Cancel;
  private Label BrushNameLable;
  private Label label5;
  private Tx3DVectorCtrl Position;
  private Label label2;
  private Label UnitLable;
  private TextBox BrushName;
  private Label label4;
  private Label label6;
  private Tx3DVectorCtrl Orientation;
  private TxObjEditBoxCtrl ParentLocation;
  private TextBox TriggerName;
  private Button Reset;
  private TextBox Distance;

  public CUiCreateTriggerDlg(CCoCreateTriggerCmd cmd, TriggerAction action)
  {
    this.m_cmd = cmd;
    this.m_placementManipulator = new TxPlacementManipulator();
    this.m_action = action;
    this.InitializeComponent();
    this.InitializeVectors();
  }

  private void InitializeVectors()
  {
    this.InitializeVector(this.Position);
    this.InitializeVector(this.Orientation);
  }

  private void InitializeVector(Tx3DVectorCtrl vectorCtrl)
  {
    vectorCtrl.Value = new TxVector()
    {
      X = 0.0,
      Y = 0.0,
      Z = 0.0
    };
  }

  internal ITxLocatableObject Manipulator
  {
    get => this.m_manipulator;
    set => this.m_manipulator = value;
  }

  public TxPlacementManipulator Placement
  {
    get => this.m_placementManipulator;
    set => this.m_placementManipulator = value;
  }

  private void Load_Dlg(object sender, EventArgs e)
  {
    TxFormSettings.RestoreFormSize((Form) this);
    this.RegisterToUnloadDocumentEvent();
    this.InitParentLocationFilters();
    this.InitParentLocation();
    this.CreateManiplutatorCones();
    this.InitTriggerData();
    this.InitType();
    this.InitTriggerName();
    this.UnitLable.Text = $"[{TxApplication.Options.Units.LinearUnitName}]";
    this.ShouldEnabledOKButton();
    this.RefreshManipulatorDisplay();
    if (this.ParentLocation.Object == null)
    {
      this.ParentLocation.Focus();
      // ISSUE: explicit non-virtual call
      __nonvirtual (((ContainerControl) this).ActiveControl) = (Control) this.ParentLocation;
    }
    else
    {
      this.Position.Focus();
      // ISSUE: explicit non-virtual call
      __nonvirtual (((ContainerControl) this).ActiveControl) = (Control) this.Position;
    }
    if (this.m_action == TriggerAction.CreateTrigger)
    {
      TxObjectList items = TxApplication.ActiveSelection.GetItems();
      if (((Collection<ITxObject>) items).Count > 0)
        this.m_origSelectedLocation = ((Collection<ITxObject>) items)[0] as ITxLocationOperation;
    }
    else if (this.m_action == TriggerAction.EditTrigger)
    {
      ((Control) this).Text = new ResourceManager("DnProcessSimulateCommands.Triggers.StringTable", ((object) this).GetType().Assembly).GetString("CMD_NAME_EDIT");
      this.TriggerType.Enabled = false;
    }
    this.UpadatePosionAndOrintationButNotManipulator();
  }

  private void InitParentLocationFilters()
  {
    // ISSUE: unable to decompile the method.
  }

  private void InitType()
  {
    ResourceManager resourceManager = new ResourceManager("DnProcessSimulateCommands.Triggers.StringTable", ((object) this).GetType().Assembly);
    this.TriggerType.Items.Add((object) resourceManager.GetString("OPEN_PAINT_GUN"));
    this.TriggerType.Items.Add((object) resourceManager.GetString("CHANGE_BRUSH"));
    this.TriggerType.Items.Add((object) resourceManager.GetString("CLOSE_PAINT_GUN"));
    if (this.m_action == TriggerAction.CreateTrigger)
    {
      this.TriggerType.SelectedIndex = 0;
    }
    else
    {
      if (this.m_action != TriggerAction.EditTrigger)
        return;
      if (this.Manipulator is TxOpenPaintGunRoboticTrigger)
        this.TriggerType.SelectedIndex = 0;
      else if (this.Manipulator is TxChangePaintGunBrushRoboticTrigger)
      {
        this.TriggerType.SelectedIndex = 1;
        this.BrushName.Text = this.m_origTriggerBrushName;
      }
      else
      {
        if (!(this.Manipulator is TxClosePaintGunRoboticTrigger))
          return;
        this.TriggerType.SelectedIndex = 2;
      }
    }
  }

  private void InitTriggerName()
  {
    if (this.m_action == TriggerAction.EditTrigger)
      this.TriggerName.Text = this.m_origTriggerName;
    this.UpdateTriggerName();
  }

  private void InitTriggerData()
  {
    if (this.m_action != TriggerAction.EditTrigger || !(this.Manipulator is TxRoboticPaintTrigger))
      return;
    TxRoboticPaintTrigger manipulator = this.Manipulator as TxRoboticPaintTrigger;
    this.m_origTriggerName = manipulator.Name;
    this.m_origTriggerTransformation = manipulator.AbsoluteLocation;
    this.m_origParentLocation = manipulator.ParentLocation;
    if (!(manipulator is TxChangePaintGunBrushRoboticTrigger))
      return;
    this.m_origTriggerBrushName = (manipulator as TxChangePaintGunBrushRoboticTrigger).BrushName;
  }

  private void UpadatePosionAndOrintationButNotManipulator()
  {
    if (this.Manipulator != null)
    {
      this._doUpdate = false;
      this.Position.Value = this.Manipulator.LocationRelativeToWorkingFrame.Translation;
      this.Orientation.Value = this.Manipulator.LocationRelativeToWorkingFrame.RotationRPY_XYZ;
      this._doUpdate = true;
    }
    else
    {
      if (this.m_action != TriggerAction.CreateTrigger)
        return;
      this.Position.Value = new TxVector();
      this.Orientation.Value = new TxVector();
    }
  }

  private void Reset_Click(object sender, EventArgs e)
  {
    if (this.m_action == TriggerAction.EditTrigger)
    {
      this.TriggerName.Text = this.m_origTriggerName;
      this.ParentLocation.Object = (ITxObject) this.m_origParentLocation;
      this.Manipulator.AbsoluteLocation = this.m_origTriggerTransformation;
      if (this.m_origTrigger is TxChangePaintGunBrushRoboticTrigger)
        this.BrushName.Text = this.m_origTriggerBrushName;
      this.Position.Focus();
      // ISSUE: explicit non-virtual call
      __nonvirtual (((ContainerControl) this).ActiveControl) = (Control) this.Position;
    }
    else if (this.m_action == TriggerAction.CreateTrigger)
    {
      this.ParentLocation.Object = (ITxObject) this.m_origParentLocation;
      this.m_IsTriggerNameChanged = false;
      this.ParentLocationChanged();
      this.TriggerType.SelectedIndex = 0;
      if (this.ParentLocation.Object == null)
      {
        this.ParentLocation.Focus();
        // ISSUE: explicit non-virtual call
        __nonvirtual (((ContainerControl) this).ActiveControl) = (Control) this.ParentLocation;
      }
      else
      {
        this.Position.Focus();
        // ISSUE: explicit non-virtual call
        __nonvirtual (((ContainerControl) this).ActiveControl) = (Control) this.Position;
      }
    }
    this.UpadatePosionAndOrintationButNotManipulator();
    this.FindPrevNextLocation();
    this.RefreshManipulatorDisplay();
    this.SetMinimalDistance();
    TxApplication.RefreshDisplay();
  }

  private void BrushName_TextChanged(object sender, EventArgs e) => this.ShouldEnabledOKButton();

  private void TriggerName_TextChanged(object sender, EventArgs e)
  {
    this.ShouldEnabledOKButton();
    this.m_IsTriggerNameChanged = true;
  }

  private void RegisterToUnloadDocumentEvent()
  {
    TxApplication.ActiveDocument.Unloading += new TxDocument_UnloadingEventHandler(this.OnUnloadDocument);
  }

  private void UnRegisterFromUnloadDocumentEvent()
  {
    TxApplication.ActiveDocument.Unloading -= new TxDocument_UnloadingEventHandler(this.OnUnloadDocument);
  }

  private void OnUnloadDocument(object sender, TxDocument_UnloadingEventArgs args)
  {
    if (this.m_secondLocation != null)
      TxDisplayableObjectEx.StopEmphasizeBlueObject(this.m_secondLocation);
    if (this.m_parentLocation != null)
      TxDisplayableObjectEx.StopEmphasizeRedObject(this.m_parentLocation);
    ((Form) this).Close();
  }

  private void Type_SelectedIndexChanged(object sender, EventArgs e)
  {
    if (this.TriggerType.SelectedIndex == 1)
    {
      this.BrushName.Enabled = true;
    }
    else
    {
      this.BrushName.Enabled = false;
      this.BrushName.Text = string.Empty;
    }
    this.ColorManipulator();
    this.ShouldEnabledOKButton();
  }

  private void Closed_Dlg(object sender, EventArgs e) => this.CleanDialogData();

  private void CleanDialogData()
  {
    if (this.m_secondLocation != null)
      TxDisplayableObjectEx.StopEmphasizeBlueObject(this.m_secondLocation);
    if (this.m_parentLocation != null)
      TxDisplayableObjectEx.StopEmphasizeRedObject(this.m_parentLocation);
    if (this.Placement != null)
      this.Placement.Hide();
    if (this.Manipulator != null && ((ITxObject) this.Manipulator).IsValid() && this.m_action == TriggerAction.CreateTrigger)
    {
      ((ITxObject) this.Manipulator).Delete();
      this.Manipulator = (ITxLocatableObject) null;
    }
    this.m_cmd.DialogIsOpen(false);
    TxFormSettings.StoreFormSize((Form) this);
    this.UnRegisterFromUnloadDocumentEvent();
    if (this.m_origSelectedLocation == null)
      return;
    TxObjectList txObjectList = new TxObjectList(1);
    ((Collection<ITxObject>) txObjectList).Add((ITxObject) this.m_origSelectedLocation);
    TxApplication.ActiveSelection.SetItems(txObjectList);
  }

  private void button_OK_Click(object sender, EventArgs e)
  {
    TxApplication.ActiveUndoManager.StartTransaction();
    if (this.m_action == TriggerAction.CreateTrigger)
    {
      if (this.ParentLocation.Object is ITxRoboticTriggerCreation triggerCreator)
      {
        TxRoboticTriggerCreationData triggerCreationData = this.InitRoboticTriggerCreationData(ref triggerCreator);
        triggerCreator.CreateRoboticTrigger(triggerCreationData);
      }
    }
    else if (this.m_action == TriggerAction.EditTrigger)
    {
      if (!this.m_origTrigger.Name.Equals(this.TriggerName.Text))
        this.m_origTrigger.Name = this.TriggerName.Text;
      if (this.m_origTrigger.ParentLocation != this.ParentLocation.Object)
        this.m_origTrigger.ParentLocation = this.ParentLocation.Object as ITxRoboticTriggerParentLocation;
      if (this.m_origTrigger is TxChangePaintGunBrushRoboticTrigger && !(this.m_origTrigger as TxChangePaintGunBrushRoboticTrigger).BrushName.Equals(this.BrushName.Text))
        (this.m_origTrigger as TxChangePaintGunBrushRoboticTrigger).BrushName = this.BrushName.Text;
    }
    TxApplication.ActiveUndoManager.EndTransaction();
    this.CleanDialogData();
    ((Form) this).Close();
  }

  private TxRoboticTriggerCreationData InitRoboticTriggerCreationData(
    ref ITxRoboticTriggerCreation triggerCreator)
  {
    string text1 = this.TriggerName.Text;
    string text2 = this.TriggerType.Text;
    TxTransformation txTransformation = this.Manipulator == null ? new TxTransformation() : this.Manipulator.AbsoluteLocation;
    TxRoboticTriggerCreationData triggerCreationData = (TxRoboticTriggerCreationData) null;
    ResourceManager resourceManager = new ResourceManager("DnProcessSimulateCommands.Triggers.StringTable", ((object) this).GetType().Assembly);
    string str1 = resourceManager.GetString("OPEN_PAINT_GUN");
    string str2 = resourceManager.GetString("CHANGE_BRUSH");
    string str3 = resourceManager.GetString("CLOSE_PAINT_GUN");
    if (text2.Equals(str3))
      triggerCreationData = (TxRoboticTriggerCreationData) new TxClosePaintGunRoboticTriggerCreationData(text1, txTransformation);
    else if (text2.Equals(str1))
      triggerCreationData = (TxRoboticTriggerCreationData) new TxOpenPaintGunRoboticTriggerCreationData(text1, txTransformation);
    else if (text2.Equals(str2))
    {
      string text3 = this.BrushName.Text;
      triggerCreationData = (TxRoboticTriggerCreationData) new TxChangePaintGunBrushRoboticTriggerCreationData(text1, text3, txTransformation);
    }
    return triggerCreationData;
  }

  private void button_Cancel_Click(object sender, EventArgs e)
  {
    if (this.m_action == TriggerAction.EditTrigger)
      this.Manipulator.AbsoluteLocation = this.m_origTriggerTransformation;
    ((Form) this).Close();
  }

  private void ParentLocationChanged()
  {
    this.DeleteManipulatorIfNeeded();
    this.UpdateParentLocationColor();
    this.UpdateTriggerName();
    this.FindPrevNextLocation();
    this.RefreshManipulatorDisplay();
    this.SetMinimalDistance();
    this.ShouldEnabledOKButton();
    this.UpadatePosionAndOrintationButNotManipulator();
  }

  private void ParentLocation_Picked(object sender, TxObjEditBoxCtrl_PickedEventArgs args)
  {
    this.ParentLocationChanged();
  }

  private void ParentLocation_TypeInvalid(object sender, EventArgs e)
  {
    this.ParentLocationChanged();
  }

  private void ParentLocation_TypeValid(object sender, EventArgs e) => this.ParentLocationChanged();

  public void UpdateTranslationOrRotationValue(TxTransformation deltaTransformation)
  {
    if (this.Manipulator == null)
      return;
    ITxLocatableObject manipulator = this.Manipulator;
    manipulator.AbsoluteLocation = TxTransformation.op_Multiply(manipulator.AbsoluteLocation, deltaTransformation);
    this.UpadatePosionAndOrintationButNotManipulator();
    this.SetMinimalDistance();
  }

  private void UpdateParentLocationColor()
  {
    if (this.m_secondLocation != null)
      TxDisplayableObjectEx.StopEmphasizeBlueObject(this.m_secondLocation);
    if (this.m_parentLocation != null)
      TxDisplayableObjectEx.StopEmphasizeRedObject(this.m_parentLocation);
    if (this.ParentLocation.Object == null)
    {
      this.m_secondLocation = (ITxDisplayableObject) null;
      this.m_parentLocation = (ITxDisplayableObject) null;
    }
    else
    {
      this.m_parentLocation = this.ParentLocation.Object as ITxDisplayableObject;
      TxDisplayableObjectEx.EmphasizeRedObject(this.m_parentLocation);
    }
  }

  private void UpdateTriggerName()
  {
    bool triggerNameChanged = this.m_IsTriggerNameChanged;
    if (this.m_action == TriggerAction.CreateTrigger && !this.m_IsTriggerNameChanged)
    {
      if (this.ParentLocation.Object != null && this.ParentLocation.Object is ITxRoboticTriggerParentLocation)
        this.TriggerName.Text = $"{this.ParentLocation.Object.Name}_tr{(((Collection<ITxObject>) (this.ParentLocation.Object as ITxRoboticTriggerParentLocation).AllRoboticTriggers).Count + 1).ToString()}";
      else
        this.TriggerName.Text = (string) null;
    }
    this.m_IsTriggerNameChanged = triggerNameChanged;
  }

  private void ColorManipulator()
  {
    if (this.m_action != TriggerAction.CreateTrigger || this.Manipulator == null)
      return;
    ResourceManager resourceManager = new ResourceManager("DnProcessSimulateCommands.Triggers.StringTable", ((object) this).GetType().Assembly);
    string text = this.TriggerType.Text;
    string str1 = resourceManager.GetString("OPEN_PAINT_GUN");
    string str2 = resourceManager.GetString("CHANGE_BRUSH");
    string str3 = resourceManager.GetString("CLOSE_PAINT_GUN");
    if (text.Equals(str3))
      (this.Manipulator as TxManipulator).SetElementColor(0, TxColor.TxColorRed);
    else if (text.Equals(str1))
      (this.Manipulator as TxManipulator).SetElementColor(0, TxColor.TxColorDarkGreen);
    else if (text.Equals(str2))
      (this.Manipulator as TxManipulator).SetElementColor(0, TxColor.TxColorBlue);
    TxApplication.RefreshDisplay();
  }

  private void CreateManiplutatorCones()
  {
    if (this.m_action == TriggerAction.CreateTrigger)
    {
      TxManipulatorCreationData manipulatorCreationData = new TxManipulatorCreationData("trigger", new TxTransformation());
      TxManipulatorConstantConeElementData constantConeElementData = new TxManipulatorConstantConeElementData(new TxTransformation(), new TxVector(0.0, 0.0, 15.0), new TxVector(0.0, 0.0, -1.0), 7.5, 15.0);
      constantConeElementData.ScaleCenter = new TxVector(0.0, 0.0, 15.0);
      this.Manipulator = (ITxLocatableObject) TxApplication.ActiveDocument.PhysicalRoot.CreateManipulator(manipulatorCreationData);
      (this.Manipulator as TxManipulator).AddElement((TxManipulatorElementData) constantConeElementData);
      this.ColorManipulator();
      if (this.ParentLocation.Object != null && this.ParentLocation.Object is ITxLocatableObject)
      {
        this.Position.Enabled = true;
        this.Orientation.Enabled = true;
        this.Manipulator.AbsoluteLocation = (this.ParentLocation.Object as ITxLocatableObject).AbsoluteLocation;
        if (this.m_secondLocation == null)
          return;
        TxTransformation absoluteLocation = this.Manipulator.AbsoluteLocation;
        TxVector translation1 = (this.ParentLocation.Object as ITxLocatableObject).AbsoluteLocation.Translation;
        double x1 = translation1.X;
        double y1 = translation1.Y;
        double z1 = translation1.Z;
        TxVector translation2 = (this.m_secondLocation as ITxLocatableObject).AbsoluteLocation.Translation;
        double x2 = translation2.X;
        double y2 = translation2.Y;
        double z2 = translation2.Z;
        TxTransformation txTransformation = new TxTransformation();
        TxVector txVector = !this.AskTheControllerIfTheTriggerShouldBeBetweenTheParentAndItsNextLocation() ? new TxVector(x1 - x2, y1 - y2, z1 - z2) : new TxVector(x2 - x1, y2 - y1, z2 - z1);
        txVector.Normalize();
        txTransformation.MapVecToVecRotation(new TxVector(0.0, 0.0, 1.0), txVector);
        txTransformation.Translation = (this.ParentLocation.Object as ITxLocatableObject).AbsoluteLocation.Translation;
        this.Manipulator.AbsoluteLocation = txTransformation;
      }
      else
        this.ParentLocation.Object = (ITxObject) null;
    }
    else
    {
      if (this.m_action != TriggerAction.EditTrigger)
        return;
      this.Manipulator = (ITxLocatableObject) this.m_origTrigger;
    }
  }

  private void RefreshManipulatorDisplay()
  {
    try
    {
      this.UnRegisterManipulatorEvents();
      this.Placement.Hide();
      if (this.ParentLocation.Object != null && this.Manipulator == null)
        this.CreateManiplutatorCones();
      if (this.ParentLocation.Object != null && this.Manipulator != null)
      {
        this.RegisterManipulatorEvents();
        this.Placement.Show(new TxPlacementManipulatorParameters((ITxObject) this.Manipulator, (TxPlacementManipulatorPosition) 1));
      }
      if (this.ParentLocation.Object == null)
        this.DeleteManipulatorIfNeeded();
    }
    catch (Exception ex)
    {
      int num = (int) MessageBox.Show("ERROR: " + ex.Message);
    }
    TxApplication.RefreshDisplay();
    ((Control) this).Focus();
  }

  private void DeleteManipulatorIfNeeded()
  {
    if (this.Manipulator == null || !((ITxObject) this.Manipulator).IsValid() || this.m_action != TriggerAction.CreateTrigger)
      return;
    ((ITxObject) this.Manipulator).Delete();
    this.Manipulator = (ITxLocatableObject) null;
    this.Position.Enabled = false;
    this.Orientation.Enabled = false;
  }

  private void RegisterManipulatorEvents()
  {
    if (this.Placement == null)
      return;
    this.Placement.ManipulatorMoved += new TxPlacementManipulator_ManipulatorMovedEventHandler(this._manipulator_ManipulatorMoved);
  }

  private void UnRegisterManipulatorEvents()
  {
    if (this.Placement == null)
      return;
    this.Placement.ManipulatorMoved -= new TxPlacementManipulator_ManipulatorMovedEventHandler(this._manipulator_ManipulatorMoved);
  }

  private void _manipulator_ManipulatorMoved(
    object sender,
    TxPlacementManipulator_ManipulatorMovedEventArgs args)
  {
    this.UpdateTranslationOrRotationValue(args.DeltaTransformation);
  }

  private void SetMinimalDistance()
  {
    try
    {
      if (this.ParentLocation.Object != null && this.Manipulator != null)
      {
        TxVector translation1 = (this.ParentLocation.Object as ITxLocatableObject).AbsoluteLocation.Translation;
        double x1 = translation1.X;
        double y1 = translation1.Y;
        double z1 = translation1.Z;
        TxVector translation2 = this.Manipulator.AbsoluteLocation.Translation;
        double x2 = translation2.X;
        double y2 = translation2.Y;
        double z2 = translation2.Z;
        double num = Math.Sqrt(Math.Pow(x1 - x2, 2.0) + Math.Pow(y1 - y2, 2.0) + Math.Pow(z1 - z2, 2.0)) / TxApplication.Options.Units.LinearMultiplier;
        if (num < 1E-06)
          num = 0.0;
        this.Distance.Text = Convert.ToString(Math.Round(num, TxApplication.Options.Units.LinearDigitsAfterDecimalPoint));
      }
      else
        this.Distance.Text = "0";
    }
    catch (Exception ex)
    {
      int num = (int) TxMessageBox.Show("The location is not visible, please display it.", "Create Trigger", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
    }
  }

  private void UpdatePosition()
  {
    if (!this._doUpdate || this.Manipulator == null)
      return;
    this.Manipulator.LocationRelativeToWorkingFrame = new TxTransformation(new TxVector(this.Position.X, this.Position.Y, this.Position.Z), this.Orientation.Value, (TxTransformation.TxRotationType) 1);
    TxApplication.RefreshDisplay();
  }

  private void UpdateOrientation()
  {
    if (!this._doUpdate || this.Manipulator == null)
      return;
    this.Manipulator.LocationRelativeToWorkingFrame = new TxTransformation(this.Position.Value, new TxVector(this.Orientation.X, this.Orientation.Y, this.Orientation.Z), (TxTransformation.TxRotationType) 1);
    TxApplication.RefreshDisplay();
  }

  private void PositionChanged(object sender, Tx3DVectorCtrl_PickedEventArgs args)
  {
    this.UpdatePosition();
    this.SetMinimalDistance();
  }

  private void PositionChanged(object sender, EventArgs e)
  {
    this.UpdatePosition();
    this.SetMinimalDistance();
  }

  private void OrientationChanged(object sender, Tx3DVectorCtrl_PickedEventArgs args)
  {
    this.UpdateOrientation();
    this.SetMinimalDistance();
  }

  private void OrientationChanged(object sender, EventArgs e)
  {
    this.UpdateOrientation();
    this.SetMinimalDistance();
  }

  private void PositionMouseDownX(object sender, EventArgs e)
  {
    this.m_placementManipulator.SelectedElement = (TxPlacementManipulatorElement) 1;
  }

  private void PositionMouseDownY(object sender, EventArgs e)
  {
    this.m_placementManipulator.SelectedElement = (TxPlacementManipulatorElement) 4;
  }

  private void PositionMouseDownZ(object sender, EventArgs e)
  {
    this.m_placementManipulator.SelectedElement = (TxPlacementManipulatorElement) 7;
  }

  private void OrientationMouseDownX(object sender, EventArgs e)
  {
    this.m_placementManipulator.SelectedElement = (TxPlacementManipulatorElement) 2;
  }

  private void OrientationMouseDownY(object sender, EventArgs e)
  {
    this.m_placementManipulator.SelectedElement = (TxPlacementManipulatorElement) 5;
  }

  private void OrientationMouseDownZ(object sender, EventArgs e)
  {
    this.m_placementManipulator.SelectedElement = (TxPlacementManipulatorElement) 8;
  }

  public void SetOrigTrigger(TxRoboticPaintTrigger trigger) => this.m_origTrigger = trigger;

  private void SetOrigParentLocation(ITxDisplayableObject location)
  {
    if (!(location is ITxRoboticTriggerParentLocation))
      return;
    this.m_origParentLocation = location as ITxRoboticTriggerParentLocation;
  }

  private void InitParentLocation()
  {
    TxObjectList allItems = TxApplication.ActiveSelection.GetAllItems();
    if (((Collection<ITxObject>) allItems).Count == 1)
    {
      if (this.m_action == TriggerAction.CreateTrigger)
      {
        this.ParentLocation.Object = ((Collection<ITxObject>) allItems)[0];
        this.m_parentLocation = ((Collection<ITxObject>) allItems)[0] as ITxDisplayableObject;
        this.SetOrigParentLocation(this.m_parentLocation);
      }
      else if (this.m_action == TriggerAction.EditTrigger)
      {
        this.ParentLocation.Object = (ITxObject) this.m_origTrigger.ParentLocation;
        this.m_parentLocation = this.m_origTrigger.ParentLocation as ITxDisplayableObject;
      }
      TxDisplayableObjectEx.EmphasizeRedObject(this.m_parentLocation);
      this.FindPrevNextLocation();
    }
    else
    {
      TxDisplayableObjectEx.StopEmphasizeRedObject(this.m_parentLocation);
      this.m_parentLocation = (ITxDisplayableObject) null;
      this.ParentLocation.Object = (ITxObject) null;
      ((Control) this.ParentLocation).Text = string.Empty;
    }
  }

  private void FindPrevNextLocation()
  {
    if (this.m_secondLocation != null)
      TxDisplayableObjectEx.StopEmphasizeBlueObject(this.m_secondLocation);
    ITxDisplayableObject displayableObject1 = (ITxDisplayableObject) null;
    int num = -1;
    ITxLocationOperation locationOperation = this.ParentLocation.Object as ITxLocationOperation;
    ITxOrderedObjectCollection objectCollection = (ITxOrderedObjectCollection) null;
    if (locationOperation != null)
    {
      objectCollection = ((ITxObject) locationOperation).Collection as ITxOrderedObjectCollection;
      num = objectCollection.GetIndexOfChild((ITxObject) locationOperation);
    }
    if (num <= -1)
      return;
    if (num + 1 < ((ITxObjectCollection) objectCollection).Count)
    {
      displayableObject1 = objectCollection.GetChildAt(num + 1) as ITxDisplayableObject;
      if (displayableObject1 is TxRoboticSeamOperation && (displayableObject1 as TxRoboticSeamOperation).Count > 0)
        displayableObject1 = (displayableObject1 as TxRoboticSeamOperation).GetChildAt(0) as ITxDisplayableObject;
    }
    if (this.AskTheControllerIfTheTriggerShouldBeBetweenTheParentAndItsNextLocation())
    {
      if (displayableObject1 == null)
        return;
      this.m_secondLocation = displayableObject1;
      TxDisplayableObjectEx.EmphasizeBlueObject(this.m_secondLocation);
    }
    else
    {
      ITxDisplayableObject displayableObject2 = (ITxDisplayableObject) null;
      if (num == 0 && objectCollection is TxRoboticSeamOperation)
      {
        ITxContinuousOperation collection = ((ITxObject) objectCollection).Collection as ITxContinuousOperation;
        int indexOfChild = ((ITxOrderedObjectCollection) collection).GetIndexOfChild((ITxObject) objectCollection);
        if (indexOfChild > 0)
          displayableObject2 = ((ITxOrderedObjectCollection) collection).GetChildAt(indexOfChild - 1) as ITxDisplayableObject;
      }
      else if (num > 0)
        displayableObject2 = objectCollection.GetChildAt(num - 1) as ITxDisplayableObject;
      if (displayableObject2 == null)
        return;
      this.m_secondLocation = displayableObject2;
      TxDisplayableObjectEx.EmphasizeBlueObject(this.m_secondLocation);
    }
  }

  private bool AskTheControllerIfTheTriggerShouldBeBetweenTheParentAndItsNextLocation()
  {
    bool flag = true;
    try
    {
      if (this.ParentLocation.Object != null)
      {
        TxOlpControllerUtilities controllerUtilities = new TxOlpControllerUtilities();
        TxRobot robot = controllerUtilities.GetRobot((ITxOperation) (this.ParentLocation.Object as ITxLocationOperation));
        string controllerName = controllerUtilities.GetControllerName((ITxOperation) (this.ParentLocation.Object as ITxLocationOperation));
        if (controllerName != string.Empty)
        {
          if (!controllerName.Equals("default"))
          {
            ITxRoboticControllerServices implementationFromController = (ITxRoboticControllerServices) controllerUtilities.GetInterfaceImplementationFromController(controllerName, typeof (ITxRoboticControllerServices), typeof (TxControllerAttribute), "ControllerName");
            if (implementationFromController != null)
            {
              implementationFromController.Init(robot);
              if (implementationFromController.TriggerLogicalParentLocation() == 1)
                flag = false;
            }
          }
        }
      }
    }
    catch (Exception ex)
    {
    }
    return flag;
  }

  private void ShouldEnabledOKButton()
  {
    bool flag = true;
    if (this.ParentLocation.Object == null)
      flag = false;
    if (flag && this.TriggerName.Text == string.Empty)
      flag = false;
    if (flag && this.TriggerType.SelectedIndex == 1 && this.BrushName.Text == string.Empty)
      flag = false;
    this.OK.Enabled = flag;
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiCreateTriggerDlg));
    this.label7 = new Label();
    this.TriggerType = new ComboBox();
    this.TypeLable = new Label();
    this.OK = new Button();
    this.Cancel = new Button();
    this.BrushNameLable = new Label();
    this.label5 = new Label();
    this.Position = new Tx3DVectorCtrl();
    this.label2 = new Label();
    this.UnitLable = new Label();
    this.BrushName = new TextBox();
    this.label4 = new Label();
    this.label6 = new Label();
    this.Orientation = new Tx3DVectorCtrl();
    this.ParentLocation = new TxObjEditBoxCtrl();
    this.TriggerName = new TextBox();
    this.Reset = new Button();
    this.Distance = new TextBox();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this.label7, "label7");
    this.label7.Name = "label7";
    this.TriggerType.DropDownStyle = ComboBoxStyle.DropDownList;
    this.TriggerType.FormattingEnabled = true;
    componentResourceManager.ApplyResources((object) this.TriggerType, "TriggerType");
    this.TriggerType.Name = "TriggerType";
    this.TriggerType.SelectedIndexChanged += new EventHandler(this.Type_SelectedIndexChanged);
    componentResourceManager.ApplyResources((object) this.TypeLable, "TypeLable");
    this.TypeLable.Name = "TypeLable";
    componentResourceManager.ApplyResources((object) this.OK, "OK");
    this.OK.Name = "OK";
    this.OK.UseVisualStyleBackColor = true;
    this.OK.Click += new EventHandler(this.button_OK_Click);
    componentResourceManager.ApplyResources((object) this.Cancel, "Cancel");
    this.Cancel.Name = "Cancel";
    this.Cancel.UseVisualStyleBackColor = true;
    this.Cancel.Click += new EventHandler(this.button_Cancel_Click);
    componentResourceManager.ApplyResources((object) this.BrushNameLable, "BrushNameLable");
    this.BrushNameLable.Name = "BrushNameLable";
    componentResourceManager.ApplyResources((object) this.label5, "label5");
    this.label5.Name = "label5";
    this.Position.ListenToPick = true;
    componentResourceManager.ApplyResources((object) this.Position, "Position");
    this.Position.LowerBound = -1000000.0;
    ((Control) this.Position).Name = "Position";
    this.Position.ReadOnly = false;
    this.Position.StepSize = 1.0;
    this.Position.UpperBound = 1000000.0;
    this.Position.ValueType = (TxNumericEditValueType) 0;
    this.Position.X = 0.0;
    this.Position.Y = 0.0;
    this.Position.Z = 0.0;
    this.Position.ValueXChanged += new EventHandler(this.PositionChanged);
    this.Position.ValueYChanged += new EventHandler(this.PositionChanged);
    this.Position.ValueZChanged += new EventHandler(this.PositionChanged);
    this.Position.Picked += new Tx3DVectorCtrl_PickedEventHandler(this.PositionChanged);
    this.Position.MouseDownX += new EventHandler(this.PositionMouseDownX);
    this.Position.MouseDownY += new EventHandler(this.PositionMouseDownY);
    this.Position.MouseDownZ += new EventHandler(this.PositionMouseDownZ);
    componentResourceManager.ApplyResources((object) this.label2, "label2");
    this.label2.Name = "label2";
    componentResourceManager.ApplyResources((object) this.UnitLable, "UnitLable");
    this.UnitLable.Name = "UnitLable";
    this.UnitLable.Text = "mm";
    componentResourceManager.ApplyResources((object) this.BrushName, "BrushName");
    this.BrushName.Name = "BrushName";
    this.BrushName.TextChanged += new EventHandler(this.BrushName_TextChanged);
    componentResourceManager.ApplyResources((object) this.label4, "label4");
    this.label4.Name = "label4";
    componentResourceManager.ApplyResources((object) this.label6, "label6");
    this.label6.Name = "label6";
    this.Orientation.ListenToPick = true;
    componentResourceManager.ApplyResources((object) this.Orientation, "Orientation");
    this.Orientation.LowerBound = -1000000.0;
    ((Control) this.Orientation).Name = "Orientation";
    this.Orientation.ReadOnly = false;
    this.Orientation.StepSize = 1.0;
    this.Orientation.UpperBound = 1000000.0;
    this.Orientation.ValueType = (TxNumericEditValueType) 1;
    this.Orientation.X = 0.0;
    this.Orientation.Y = 0.0;
    this.Orientation.Z = 0.0;
    this.Orientation.ValueXChanged += new EventHandler(this.OrientationChanged);
    this.Orientation.ValueYChanged += new EventHandler(this.OrientationChanged);
    this.Orientation.ValueZChanged += new EventHandler(this.OrientationChanged);
    this.Orientation.Picked += new Tx3DVectorCtrl_PickedEventHandler(this.OrientationChanged);
    this.Orientation.MouseDownX += new EventHandler(this.OrientationMouseDownX);
    this.Orientation.MouseDownY += new EventHandler(this.OrientationMouseDownY);
    this.Orientation.MouseDownZ += new EventHandler(this.OrientationMouseDownZ);
    this.ParentLocation.KeepFaceEmphasizedWhenControlIsNotFocused = true;
    this.ParentLocation.ListenToPick = false;
    componentResourceManager.ApplyResources((object) this.ParentLocation, "ParentLocation");
    ((Control) this.ParentLocation).Name = "ParentLocation";
    this.ParentLocation.Object = (ITxObject) null;
    this.ParentLocation.PickAndClear = false;
    this.ParentLocation.PickLevel = (TxPickLevel) 1;
    this.ParentLocation.PickOnly = false;
    this.ParentLocation.ReadOnly = false;
    this.ParentLocation.ValidatorType = (TxValidatorType) 1;
    this.ParentLocation.TypeInvalid += new EventHandler(this.ParentLocation_TypeInvalid);
    this.ParentLocation.TypeValid += new EventHandler(this.ParentLocation_TypeValid);
    this.ParentLocation.Picked += new TxObjEditBoxCtrl_PickedEventHandler(this.ParentLocation_Picked);
    componentResourceManager.ApplyResources((object) this.TriggerName, "TriggerName");
    this.TriggerName.Name = "TriggerName";
    this.TriggerName.TextChanged += new EventHandler(this.TriggerName_TextChanged);
    componentResourceManager.ApplyResources((object) this.Reset, "Reset");
    this.Reset.Name = "Reset";
    this.Reset.UseVisualStyleBackColor = true;
    this.Reset.Click += new EventHandler(this.Reset_Click);
    componentResourceManager.ApplyResources((object) this.Distance, "Distance");
    this.Distance.Name = "Distance";
    this.Distance.ReadOnly = true;
    componentResourceManager.ApplyResources((object) this, "$this");
    ((ContainerControl) this).AutoScaleMode = AutoScaleMode.Font;
    ((Control) this).Controls.Add((Control) this.Distance);
    ((Control) this).Controls.Add((Control) this.Reset);
    ((Control) this).Controls.Add((Control) this.TriggerName);
    ((Control) this).Controls.Add((Control) this.ParentLocation);
    ((Control) this).Controls.Add((Control) this.label6);
    ((Control) this).Controls.Add((Control) this.Orientation);
    ((Control) this).Controls.Add((Control) this.label4);
    ((Control) this).Controls.Add((Control) this.BrushName);
    ((Control) this).Controls.Add((Control) this.UnitLable);
    ((Control) this).Controls.Add((Control) this.label2);
    ((Control) this).Controls.Add((Control) this.Position);
    ((Control) this).Controls.Add((Control) this.label5);
    ((Control) this).Controls.Add((Control) this.OK);
    ((Control) this).Controls.Add((Control) this.TypeLable);
    ((Control) this).Controls.Add((Control) this.TriggerType);
    ((Control) this).Controls.Add((Control) this.label7);
    ((Control) this).Controls.Add((Control) this.BrushNameLable);
    ((Control) this).Controls.Add((Control) this.Cancel);
    ((Form) this).FormBorderStyle = FormBorderStyle.FixedDialog;
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = nameof (CUiCreateTriggerDlg);
    ((Form) this).FormClosing += new FormClosingEventHandler(this.Closed_Dlg);
    ((Form) this).Load += new EventHandler(this.Load_Dlg);
    ((Control) this).ResumeLayout(false);
    ((Control) this).PerformLayout();
  }
}
